import java.util.HashMap;
import java.util.Iterator;

/**
* @author Simon Symeonidis
* Small datastructure class that will help with things.
*/

class SpimiDictionary{
	
  private HashMap<String, PostingsList> map = new HashMap();
  
  /** Class constructor */
  public SpimiDictionary(){}

  /** This is to clear the values from the hasmap */
  public void clear(){
    map.clear();
  }

  public void addTerm(String theTerm, String docID){
    //System.out.print("D");
    /** Key exists, add to postings list  */
    if ( map.containsKey(theTerm) ){
      map.get(theTerm).addToPostingList(docID); 
      //System.out.println("old term : " + theTerm + " with postings list " + map.get(theTerm));
    }
    /** Key does not exist, create term, add to postings list */
    else {
      PostingsList p = new PostingsList(docID); 
      map.put(theTerm, p);
      //System.out.println("new term : " + theTerm + " with postings list: " + p);
    }
  }
  public PostingsList theList(String term){
  	return map.get(term);
  }

  public String toString(){ 
    Iterator it;    
    it = map.keySet().iterator();
    String str  = new String(); 
    String term = new String();
    
    str  = "";
    term = "";

    while(it.hasNext()){
      term = (String)it.next(); 
      //System.out.println("key " + term  + " map.get " + map.get(term));
      str += term;
      str += " : " + map.get(term).displayContents();
      str += "\n";
    }

    return str;
  }

}
